/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ile.VISTA;

import ile.DAO.MarcaMDAO;
import ile.DAO.MaterialDAO;
import ile.DAO.OrdenTrabajoDAO;
import ile.DAO.PedidoMaterialDAO;
import ile.DAO.UsuarioDAO;
import ile.ModeloTabla.ListadePedidosMaterialModelTable;
import ile.Modelo.Marka;
import ile.Modelo.Materiales;
import ile.Modelo.OrdenTrabajo;
import ile.Modelo.PedidoMaterial;
import ile.Modelo.Usuario;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author 
 */
public class PedidosMateriales extends javax.swing.JDialog {
    ListadePedidosMaterialModelTable lpmmt;
    List<PedidoMaterial> pedidosMs;
   PedidoMaterial pedidoSeleccionado;
    
    Usuario usuario;
    
    List<OrdenTrabajo> ordenTrabajos;
    /**
     * Creates new form PedidoHerramientas
     */
    public PedidosMateriales(java.awt.Frame parent, boolean modal, Usuario u) {
        super(parent, modal);
        lpmmt = new ListadePedidosMaterialModelTable();
        pedidosMs = new ArrayList<PedidoMaterial>();
        
        usuario=u;
         
        ordenTrabajos=usuario.getOrdenTrabajos();
        initComponents();
       
        lblUsuario.setText(usuario.getNombre());
       // setMaximumSize(Integer.MAX_VALUE);
       //setSize(new DimensionUIResource(Integer.MAX_VALUE,Integer.MAX_VALUE));
   
        setLocationRelativeTo(null);
    }

    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jPanel1 = new javax.swing.JPanel();
        lblUsuario = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        listaPedidosM = new javax.swing.JTable();
        btnElimnarPH = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        OTSm = new javax.swing.JComboBox();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tablaMateriales = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        txtNomMaterial = new javax.swing.JTextField();
        btnBuscarM = new javax.swing.JButton();
        btnOkH = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        lblImagen = new javax.swing.JLabel();
        btnCancelar = new javax.swing.JButton();
        btnAceptarH = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(126, 166, 189));
        jPanel1.setAutoscrolls(true);

        jPanel2.setBackground(new java.awt.Color(126, 166, 189));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(java.awt.Color.white, 2, true), "Lista de Pedidos de Materiales", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Serif", 1, 16))); // NOI18N

        listaPedidosM.setBackground(new java.awt.Color(225, 254, 210));
        listaPedidosM.setModel(lpmmt);
        jScrollPane2.setViewportView(listaPedidosM);

        btnElimnarPH.setFont(new java.awt.Font("DejaVu Serif", 1, 15)); // NOI18N
        btnElimnarPH.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Borrar.png"))); // NOI18N
        btnElimnarPH.setText("Eliminar");
        btnElimnarPH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnElimnarPHActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("DejaVu Serif", 1, 23)); // NOI18N
        jLabel1.setForeground(java.awt.Color.black);
        jLabel1.setText("Numero OT:");

        OTSm.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${ordenTrabajos}");
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, OTSm);
        bindingGroup.addBinding(jComboBoxBinding);
        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${pedidoM.otm}"), OTSm, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(OTSm, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(283, 283, 283))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 808, Short.MAX_VALUE))
                .addGap(15, 15, 15)
                .addComponent(btnElimnarPH, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(OTSm, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 37, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnElimnarPH, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32))
        );

        jPanel4.setBackground(new java.awt.Color(126, 166, 189));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(java.awt.Color.white, 2, true), "Lista de Materiales", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("DejaVu Serif", 1, 16))); // NOI18N

        tablaMateriales.setBackground(new java.awt.Color(225, 254, 210));
        tablaMateriales.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        tablaMateriales.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tablaMateriales.setGridColor(new java.awt.Color(5, 5, 5));
        tablaMateriales.setSelectionBackground(new java.awt.Color(151, 199, 234));
        tablaMateriales.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaMaterialesMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tablaMateriales);

        jLabel3.setFont(new java.awt.Font("DejaVu Serif", 1, 23)); // NOI18N
        jLabel3.setForeground(java.awt.Color.black);
        jLabel3.setText("Nombre:");

        txtNomMaterial.setFont(new java.awt.Font("DejaVu Serif", 0, 22)); // NOI18N
        txtNomMaterial.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtNomMaterialMouseClicked(evt);
            }
        });
        txtNomMaterial.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNomMaterialKeyTyped(evt);
            }
        });

        btnBuscarM.setFont(new java.awt.Font("DejaVu Serif", 1, 15)); // NOI18N
        btnBuscarM.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/view.png"))); // NOI18N
        btnBuscarM.setText("Buscar");
        btnBuscarM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarMActionPerformed(evt);
            }
        });

        btnOkH.setFont(new java.awt.Font("DejaVu Serif", 1, 15)); // NOI18N
        btnOkH.setText("Ok");
        btnOkH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkHActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("DejaVu Serif", 1, 20)); // NOI18N
        jLabel4.setText("Gráfica");

        lblImagen.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNomMaterial)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBuscarM, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 806, Short.MAX_VALUE))
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(jLabel4))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lblImagen, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                            .addComponent(btnOkH, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(txtNomMaterial, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnBuscarM, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblImagen, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnOkH, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        btnCancelar.setFont(new java.awt.Font("DejaVu Serif", 1, 15)); // NOI18N
        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/cancelar.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnAceptarH.setFont(new java.awt.Font("DejaVu Serif", 1, 15)); // NOI18N
        btnAceptarH.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/ok.png"))); // NOI18N
        btnAceptarH.setText("Aceptar");
        btnAceptarH.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarHActionPerformed(evt);
            }
        });

        jLabel2.setText("Usuario:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 681, Short.MAX_VALUE)
                        .addComponent(btnAceptarH, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 414, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lblUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 335, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAceptarH, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(22, 22, 22)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(429, Short.MAX_VALUE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtNomMaterialMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtNomMaterialMouseClicked
        // TODO add your handling code here:
        
      new TecladoVirtual(null, true, txtNomMaterial, tablaMateriales,"PedidosM").setVisible(true);
    }//GEN-LAST:event_txtNomMaterialMouseClicked

    private void btnOkHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkHActionPerformed
        // TODO add your handling code here:
        DefaultTableModel modelo=(DefaultTableModel) tablaMateriales.getModel();
        for (int i = 0; i < modelo.getRowCount(); i++) {
           String cadena=String.valueOf(tablaMateriales.getValueAt(i, 6));
            Boolean bandera= Boolean.valueOf(cadena);
                 if(bandera==true){     

                     Marka marka= marcasM.get(i);
                    pedidoSeleccionado= new PedidoMaterial();
                     
                     try {
                         
                      String cantidad = String.valueOf(tablaMateriales.getValueAt(i, 5));
                     int cant = Integer.valueOf(cantidad);
                     pedidoSeleccionado.setCantidad(cant);
                     Date date = new Date();
                     DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                     pedidoSeleccionado.setFecha(dateFormat.format(date));
                     
                      int cantidadResta=marka.getDisponibleM()-cant;                       
                        tablaMateriales.setValueAt(cantidadResta, i, 4);
                        marka.setDisponibleM(cantidadResta);
                        
                        pedidoSeleccionado.setMarka(marka);
                        pedidosMs.add(pedidoSeleccionado);
                        
                        } catch (Exception e) {
                            JOptionPane.showMessageDialog(null, "Existen Campos Vacíos");
                     }

          
                        ordenTrabajos=usuario.getOrdenTrabajos();
                 }else{
                    String cad=String.valueOf(tablaMateriales.getValueAt(i, 5));
                    if(bandera==false && !cad.equals("")){
                        JOptionPane.showMessageDialog(null, "Existen Campos Vacíos");
                        break;
                    }
                    

                 }

        }
        
        lpmmt= (ListadePedidosMaterialModelTable) listaPedidosM.getModel();
        lpmmt.setFilas(pedidosMs);


          
        for (int i = 0; i < tablaMateriales.getRowCount(); i++) {
           tablaMateriales.setValueAt(false,i, 6);
           tablaMateriales.setValueAt("",i, 5);

        }
        
        //modelo= null;
        
     // lpmt.setPedido(null);
    }//GEN-LAST:event_btnOkHActionPerformed

    private void btnElimnarPHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnElimnarPHActionPerformed
        // TODO add your handling code here:
        ListadePedidosMaterialModelTable pedidosMT =  (ListadePedidosMaterialModelTable) listaPedidosM.getModel();
       pedidoSeleccionado = pedidosMT.getPedidoMaterial();
        List<PedidoMaterial> listaPedidos = pedidosMT.getFilas();
        if (pedidoSeleccionado != null) {
            int rta = JOptionPane.showConfirmDialog(this, "Desea realmente eliminar el Pedido? "
                    + pedidoSeleccionado.getMarka().getMaterial(), "Eliminar", JOptionPane.YES_NO_OPTION);

            if (rta == JOptionPane.YES_OPTION) {
                listaPedidos.remove(pedidoSeleccionado);
                pedidosMT.setFilas(listaPedidos);
                JOptionPane.showMessageDialog(this, "Pedido eliminado", "Información", JOptionPane.INFORMATION_MESSAGE);

            }

        }else{
            JOptionPane.showMessageDialog(this, "Debe seleccionar un Pedido de la tabla", "Error", JOptionPane.ERROR_MESSAGE);
        }
        
       // pedidosMT.setPedidoMaterial(null);
 
    }//GEN-LAST:event_btnElimnarPHActionPerformed

    private void btnBuscarMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarMActionPerformed
        // TODO add your handling code here:
        cargarTablaMateriales();
    }//GEN-LAST:event_btnBuscarMActionPerformed

    private void txtNomMaterialKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNomMaterialKeyTyped
        // TODO add your handling code here:
        cargarTablaMateriales();
    }//GEN-LAST:event_txtNomMaterialKeyTyped

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnAceptarHActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarHActionPerformed
        
        OrdenTrabajo otSeleccionada=(OrdenTrabajo) OTSm.getSelectedItem();
        ListadePedidosMaterialModelTable materialModelTable= (ListadePedidosMaterialModelTable) listaPedidosM.getModel();
        pedidoSeleccionado= materialModelTable.getPedidoMaterial();
         
        
        if(pedidoSeleccionado!= null){

          for (int i = 0; i < pedidosMs.size(); i++) {
              
                         
                PedidoMaterial pm= pedidosMs.get(i);
                Marka marka= pm.getMarka();
                
               
                PedidoMaterialDAO pmdao= new PedidoMaterialDAO(pm);
                
                     marka.agregarPM(pm);
                     otSeleccionada.agregarOTM(pedidoSeleccionado);
                     usuario.agregarPM(pm);
                     
                     MarcaMDAO marcaMDAO= new MarcaMDAO(marka);
                     OrdenTrabajoDAO dAO= new OrdenTrabajoDAO(otSeleccionada);
                     UsuarioDAO udao= new UsuarioDAO(usuario);
                            
             try {
                    pmdao.persist();
                    marcaMDAO.update();
                    dAO.update();
                    udao.update();
                    
                  
                    dispose();
            } catch (Exception ex) {
                Logger.getLogger(PedidosMateriales.class.getName()).log(Level.SEVERE, null, ex);
            }
           
                 }
        JOptionPane.showMessageDialog(null, "Pedido almacenado satisfactoriamente");
        dispose();
        }else{
           
           JOptionPane.showMessageDialog(this, "No ha realizado ningun pedido", "Error", JOptionPane.ERROR_MESSAGE);
           dispose();
        }

    }//GEN-LAST:event_btnAceptarHActionPerformed

    private void tablaMaterialesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMaterialesMouseClicked
        // TODO add your handling code here:
//        
//        if (tablaMateriales.getRowCount() > 0) {
//            int aux = tablaMateriales.getSelectedRow();
//            if (aux != -1) {
//                material = (Material) materials.get(aux);
//                CargarImagenes ci = new CargarImagenes();
//                try {
//                    Image im = ci.getfoto(String.valueOf(material.getId()));
//                    lblImagen.setIcon(new ImageIcon(im));
//                } catch (SQLException ex) {
//                    Logger.getLogger(PedidosMateriales.class.getName()).log(Level.SEVERE, null, ex);
//                }
//            } else {
//                JOptionPane.showMessageDialog(this, "No existen datos en la tabla ...", "Material", JOptionPane.WARNING_MESSAGE);
//            }
//        } else {
//            
//        }
//        
//        tablaMateriales=null;
    }//GEN-LAST:event_tablaMaterialesMouseClicked

    List<Marka> marcasM;
    List<Materiales> materials=null;
    Materiales material=null;
    public void cargarTablaMateriales(){
        
        
        MaterialDAO materialDAO = new MaterialDAO(null);
         materials= materialDAO.buscarPorNombre(txtNomMaterial.getText());
      
        marcasM= new ArrayList<Marka>();
        for (int i = 0; i <materials.size(); i++) {
            //agregar a la lista de marcas el material que pertenece a esa marca
            marcasM.addAll(materials.get(i).getMarkas());
         
        }

        DefaultTableModel TableModel = new DefaultTableModel(){
            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                if(columnIndex==5 || columnIndex==6){
                return true;
                }else{
                return false;
                }

             }

            @Override
            public Class getColumnClass(int columna){
            if(columna==5){
            return Integer.class;
            }if(columna==6){
                return Boolean.class;
            }
            return Class.class;
    }
        };
        
        
        
        String []  columnaNombre={"Material","Código","Marca","Total", "Disponibilidad", "Cantidad a Pedir", "Selección"};
        TableModel.setColumnIdentifiers(columnaNombre);
        Object[] fila=new Object[TableModel.getColumnCount()];
        for (int i = 0; i < marcasM.size(); i++) {
           fila[0]=marcasM.get(i).getMaterial().getNombre();
           fila[1]=marcasM.get(i).getMaterial().getCodigo();
           fila[2]=marcasM.get(i).getNombreM();
           fila[3]= marcasM.get(i).getCantidadM();
           fila[4]= marcasM.get(i).getDisponibleM();
           fila[5]= "";
     
         TableModel.addRow(fila);
        }
      tablaMateriales.setModel(TableModel);

        TableModel= null;
          
}
    /**
     * @param args the command line arguments
     */
 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox OTSm;
    private javax.swing.JButton btnAceptarH;
    private javax.swing.JButton btnBuscarM;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnElimnarPH;
    private javax.swing.JButton btnOkH;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel lblImagen;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JTable listaPedidosM;
    private javax.swing.JTable tablaMateriales;
    private javax.swing.JTextField txtNomMaterial;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

public PedidoMaterial getPedidoM() {
        return pedidoSeleccionado;
    }

    public void setPedidoM(PedidoMaterial pedidoM) {
        this.pedidoSeleccionado = pedidoM;
    }

    public List<OrdenTrabajo> getOrdenTrabajos() {
        return ordenTrabajos;
    }

    public void setOrdenTrabajos(List<OrdenTrabajo> ordenTrabajos) {
        this.ordenTrabajos = ordenTrabajos;
    }

    
    
}
